package com.allwees.bs.module.oms.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.allwees.bs.core.model.order.entity.OrderItemModel;
import com.allwees.bs.core.model.order.wf.order.EOrderItemAction;
import com.allwees.bs.core.model.order.wf.order.EOrderItemState;
import com.allwees.bs.core.modelbase.entity.WfBaseEntity;
import com.allwees.core.common.util.reflect.PojoUtil;
import com.allwees.core.wf.Workflow;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@TableName("o_order_item")
public class OrderItem extends OrderItemModel implements Workflow<EOrderItemState, EOrderItemAction> {
    @Column(columnDefinition = "varchar(255)")
    @Enumerated(EnumType.STRING)
    private EOrderItemAction latestAction;

    @Column(columnDefinition = "varchar(255)")
    @Enumerated(EnumType.STRING)
    private EOrderItemState backState;

    @Column(columnDefinition = "varchar(255)")
    @Enumerated(EnumType.STRING)
    private EOrderItemState state;

    /**
     * 是否可退货
     */
    protected Boolean refundAble;

    private String defNo;

    public OmsOrderItem convertToOmsOrderItem() {
        OmsOrderItem omsOrderItem = PojoUtil.copyWithOmit(new OmsOrderItem(), this, WfBaseEntity.fields);
        omsOrderItem.setUuid(this.getUuid());
        omsOrderItem.setId(this.getId());
        return omsOrderItem;
    }

    public String getStateDesc() {
        return state.customerDesc();
    }

    /**
     * COD订单取消状态时获取原状态
     *
     * @return
     */
    public String getBackStateDesc(){
        if (latestAction == null || backState == null) {
            return null;
        }
        if( latestAction.equals(EOrderItemAction.USER_COD_CANCEL_ON_LIMIT) && backState.equals(EOrderItemState.WAIT_SHIP)) {
            return "待发货(冻结期内)";
        } else if (latestAction.equals(EOrderItemAction.USER_COD_CANCEL_AFTER_LIMIT) && backState.equals(EOrderItemState.WAIT_SHIP) ) {
            return "待发货(冻结期外)";
        }
        return backState.customerDesc();
    }

    @Override
    public String getBsKey() {
        return uuid;
    }
}
